package com.ruoyi.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.BusinessSys;
import com.ruoyi.common.core.domain.entity.Person;
import com.ruoyi.system.service.*;
import com.ruoyi.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(value="/jsyc")
public class JsycController {


    @Autowired
    private CQYCSignService signService;
    @Autowired
    private IBusinessSysService businessSysService;
    @Autowired
    private IPersonService personService;
    @Autowired
    private IDockingErrService dockingErrService;
    @Resource
    private AsyncSignService asyncSignService;

//    /**
//     * PDF文档验章接口，接口
//     */
//    @RequestMapping(value ="/pdf/retail")
//    @ResponseBody
//    public String extractRevision(@RequestBody JSONObject data) throws Exception {
//        JSONObject result;
//        String documentData;
//        String businessSys;
//        //首先确认参数
//        if(data.containsKey("documentData")&&data.containsKey("businessSys")){
//            documentData = data.getString("documentData");
//            businessSys = data.getString("businessSys");
//        }
//
//        //默认异步调用
//        boolean isAsync = false;
//        if (dataJs.containsKey("isAsync")){
//            isAsync = dataJs.getBoolean("isAsync");
//        }
//        JSONArray seals =dataJs.getJSONArray("seals");
//        //检查前置参数
//        result = ParameterCheck(businessSys,signUser,documentCode);
//        if (!result.getString("code").equals("0000")){
//            //如果签章失败添加错误日志
//            dockingErrService.insertDockingErrSign(businessSys,
//                    result.getString("msg")+"&code:"+result.getString("code"),"异步签章");
//            return result.toJSONString();
//        }
//        String event ="同步签章";
//        if(isAsync){
//            //异步签章
//            asyncSignService.asyncSignPDF(seals,documentCode,businessSys,signUser);
//            result.put("code","0000");
//            result.put("msg","签章任务添加成功");
//            event = "异步签章";
//        }else {
//            //同步签章
//            result = signService.signPDF(seals,documentCode,businessSys,signUser);
//        }
//        if (!"0000".equals(result.getString("code"))){     //查找文档
//            //如果签章失败添加错误日志
//            dockingErrService.insertDockingErrSign(businessSys,
//                    result.getString("msg")+"&code:"+result.getString("code"),event);
//        }
//        return result.toJSONString();
//    }
    /**
     * 对签章前置参数进行检查
     * @param businessSys 业务系统ID
     * @param signUser 签章人
     * @param documentCode 文档编码
     * @return  检查结果
     */
    public JSONObject ParameterCheck(String businessSys, String signUser,String documentCode){
        JSONObject result = new JSONObject();
        //查找业务系统名称
        BusinessSys business_system  = businessSysService.selectBusinessSysByBSysId(businessSys);
        if (business_system==null){
            result.put("code","0002");
            result.put("msg","找不到业务系统");
            return result;
        }
        //查找签章人员
        Person signPer = personService.findPersonBySYS(businessSys,signUser);
        if (signPer==null){
            result.put("code","0001");
            result.put("msg","找不到签章人");
            return result;
        }
        result.put("code","0000");
        result.put("msg","参数检查通过");
        return result;
    }
}
